<?php

namespace Housing;

use C\L\WebUserController;

class SigninController extends WebUserController
{
    protected function init()
    {
        $this->service = $this->s_signin;
        $this->hideKeys = [
        'is_delete'
        ];
    }

    public function indexAction()
    {
        $res = $this->service->sign($this->uid);
        if ($res) {
            $this->success($res);
        }
        $this->error($this->message->getSerMsg());
    }

    public function listAction()
    {
        $sdate = $this->getValue('sdate', false, 'int');
        $edate = $this->getValue('edate', false, 'int');
        if (!$sdate or !$edate) {
            $sdate = date('Ym') . '01';
            $edate = date('Ymt');
        }
        $date_list = $this->s_signin->getDateList($sdate, $edate);
        $today = date('Ymd');

      //已设置列表
        $where = ' type=1 and day between ' . $sdate . ' and ' . $edate;
        $order = ' day asc';
        $sys_signin_list = $this->s_syssignin->getContinuityList($where, $order);
        $sys_signin = [];
        $today_reward = [];
        foreach ($sys_signin_list as &$v) {
            if ($v['reward_type'] == 1) {
                $v['coupon'] = [];
            }
            $sys_signin[$v['day']] = $v;
            if ($v['day'] == $today) {
                $today_reward = $v;
            }
        }
        unset($v);

        $addtimeArr = [strtotime($sdate), strtotime($edate) + (24 * 3600) - 1];
        $user_sign_list = $this->di['s_signin']->searchAll(['u_id' => $this->uid, 'addtime' => $addtimeArr], ['addtime' => 'between'], [], 'addtime asc');
        $is_signin = 0;
        foreach ($user_sign_list as $v) {
            $date = date('Ymd', $v['addtime']);
            if ($date == $today) {
                $is_signin = 1;
            }
            $user_signin[$date] = $v;
        }
        unset($v);
        $user_sign_reward_list = $this->di['s_signin_user_reward']->searchAll(['uid' => $this->uid, 'addtime' => $addtimeArr], ['addtime' => 'between'], [], 'addtime asc');
        $user_signin_reward = [];
        foreach ($user_sign_reward_list as &$v) {
            $v['reward'] = json_decode($v['reward'], true);
            if ($v['reward']['reward_type'] == 1) {
                $v['reward']['coupon'] = [];
            }
            $date = date('Ymd', $v['addtime']);
            $user_signin_reward[$date] = $v;
        }
        unset($v);

        $res = [];
        foreach ($date_list as $k => $date) {
            $res[$k]['date'] = $date;
            $res[$k]['is_signin'] = isset($user_signin[$date]) ? 1 : 0;
            $res[$k]['reward'] = isset($sys_signin[$date]) ? $sys_signin[$date] : [];
            $res[$k]['reward_is_send'] = 0;

            if (isset($user_signin_reward[$date])) {
                $res[$k]['reward'] = $user_signin_reward[$date]['reward'];
                $res[$k]['reward_is_send'] = 1;
            }
        }
        unset($k, $date);

        $data = [];
        $data['is_signin'] = $is_signin;
        $data['date_list'] = $res;
        $data['days'] = count($user_sign_list);
        $data['today_reward'] = $today_reward;
        $this->success($data);
    }

    public function getSignDays()
    {
      //连续签到天数，不是累计不是累计
        $last_info = $this->di['s_signin']->search(['u_id' => $this->uid], [], [], 'addtime desc');

        $last_date = date('Ymd', $last_info['addtime']);
        $date = date('Ymd');

        if (!$last_info) {
            $days = 0;
        } else {
            if (($date - $last_date) <= 1) {
                $days = $last_info['days'];
            } else {
                $days = 1;
            }
        }

        return $days;
    }

    public function rowAction()
    {
        $where = ' type=2';
        $where .= ' and day =' . date('Ym');
        $order = 'days asc';
        $reward_list = $this->s_syssignin->getContinuityList($where, $order);
        if (!$reward_list) {
            $this->success([]);
        }

        $stime = strtotime(date('Ym') . '01');
        $etime = strtotime(date('Ymt')) + 24 * 3600 - 1;
        $conditions = ' u_id =' . $this->uid;
        $conditions .= " and addtime between $stime and $etime";
        $user_signin_days = $this->s_signin->thisMonthSignin($conditions);
        $where = ' uid =' . $this->uid;
        $where .= " and addtime between $stime and $etime";
        $user_month_reward = $this->s_signin_user_reward->thisMonthSigninReward($where);

        foreach ($reward_list as &$v) {
            $can_draw = 0; //不可领
            if ($user_signin_days >= $v['days']) {
                $can_draw = 1; //可领
            }
            if (isset($user_month_reward[$v['id']])) {
                $can_draw = 2; //已领取
                $draw = $user_month_reward[$v['id']];
                $v['reward_type'] = $draw['reward']['reward_type'];
                $v['reward_num'] = $draw['reward']['reward_num'];
                if ($v['reward_type'] == 2) {
                    $v['coupon'] = $draw['reward']['coupon'];
                }
            }
            if ($v['reward_type'] == 1) {
                unset($v['coupon']);
            }
            $v['can_draw'] = $can_draw;
        }

        $res['signin_days'] = $user_signin_days;
        $res['reward_list'] = $reward_list;
        $this->success($res);
    }

    public function receiveAction()
    {
        $sys_sign_id = $this->getValue('reward_id', true, 'int');

        $res = $this->s_signin->sendReward($this->uid, $sys_sign_id);
        if ($res) {
            $this->success($res);
        }

        $this->error($this->message->getSerMsg());
    }
}
